package com.byt.consultation.model;

import com.byt.base.model.BaseModel;
import com.byt.common.bean.ConsultaBean;
import com.byt.consultation.api.ConsultationApi;
import com.byt.consultation.bean.BageNum;
import com.byt.consultation.bean.MainBanner;
import com.byt.netapi.bean.ResponseBean;
import com.byt.netapi.exception.ExceptionHandle;
import com.byt.netapi.observer.BaseObserver;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsultModel extends BaseModel<List<ConsultaBean>> {

    private long memberId;
    private long infoId;

    public ConsultModel(long memberId, long infoId) {
        this.memberId = memberId;
        this.infoId = infoId;
    }

    @Override
    public void refresh() {
        load();
        onBannerImg();
        onBageNum();
    }

    @Override
    protected void load() {
        Map<String,Object>  map = new HashMap<>();
        ConsultationApi.getInstance().getConsultList(new BaseObserver<ResponseBean<List<ConsultaBean>>>(this) {
            @Override
            public void onNext(ResponseBean<List<ConsultaBean>> enity) {
                loadSuccess(enity.getData());
            }

            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                e.printStackTrace();
                loadFail(e.getCause().getMessage());
            }
        },map);
    }

    /**
     * 获取广告图
     */
    public void onBannerImg() {
        Map<String,Object>  map = new HashMap<>();
        ConsultationApi.getInstance().getMainBanner(new BaseObserver<ResponseBean<List<MainBanner>>>(this) {
            @Override
            public void onNext(ResponseBean<List<MainBanner>> enity) {
                synchronized (this) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for (WeakReference<IBaseModelListener> weakListener : mWeakListenerArrayList) {
                                if (weakListener.get() instanceof IModelListener) {
                                    IConsultModelListener listenerItem = (IConsultModelListener) weakListener.get();
                                    if (listenerItem != null) {
                                        listenerItem.onBannerImg(enity.getData());
                                    }
                                }
                            }
                        }
                    },0);
                }
            }
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                e.printStackTrace();
                loadFail(e.getCause().getMessage());
            }
        },map);
    }

    /**
     * 获取广告图
     */
    public void onBageNum() {
        Map<String,Object>  map = new HashMap<>();
        ConsultationApi.getInstance().getBageNum(new BaseObserver<ResponseBean<BageNum>>(this) {
            @Override
            public void onNext(ResponseBean<BageNum> enity) {
                synchronized (this) {
                    mUiHandler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            for (WeakReference<IBaseModelListener> weakListener : mWeakListenerArrayList) {
                                if (weakListener.get() instanceof IModelListener) {
                                    IConsultModelListener listenerItem = (IConsultModelListener) weakListener.get();
                                    if (listenerItem != null) {
                                        listenerItem.onBageNum(enity.getData());
                                    }
                                }
                            }
                        }
                    },0);
                }
            }
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                e.printStackTrace();
            }
        },map);
    }

    public interface IConsultModelListener<T> extends IModelListener<T> {

        void onBannerImg(List<MainBanner> banners);

        void onBageNum(BageNum bageNum);


    }

}
